package tt.dz.biz.dao.menu;

import java.util.List;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.vo.MenuVO;
import tt.dz.vo.RoleMenuVO;
import tt.dz.vo.RoleVO;

@Repository("menuDAO")
public class MenuDAOImpl
  extends BaseDAO
{
  private static final String QUERY_MENU_BY_ROLE = "select  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM ms_sec_role_menu AS srm LEFT JOIN ms_sec_menu AS sm ON sm.menu_id = srm.menu_id  WHERE srm.role_id = :roleId AND srm.is_valid = 1 and sm.is_valid = 1 ORDER BY sm.sort asc";
  private static final String QUERY_MENU_BY_ROLELIST = "select  distinct  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM ms_sec_role_menu AS srm ,  ms_sec_menu AS sm  where  sm.menu_id = srm.menu_id  and  srm.role_id in ( :roleIdList) AND srm.is_valid = 1 and sm.is_valid = 1  ORDER BY sm.sort  asc ";
  private static final String INSERT_ROLE_MENU = "insert into ms_sec_role_menu(role_id,menu_id,created_by,created_time,updated_by,updated_time,is_valid,sys_code) values(:roleId,:menuId,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid,:sysCode)";
  private static final String DELETE_ROLE_MENU = "update ms_sec_role_menu set is_valid = 0 where role_id = :roleId and menu_id = :menuId";
  private static final String UPDATE_ROLE_MENU = "update ms_sec_role_menu set is_valid = 1 where role_id = :roleId and menu_id = :menuId";
  private static final String QUERY_ROLE_MENU_COUNT = "select count(1) from ms_sec_role_menu where role_id = :roleId and menu_id = :menuId";
  private static final String QUERY_ALL_MENU = "select  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM  ms_sec_menu AS sm where is_valid = 1 order by  sort  asc ";
  
  public List<MenuVO> queryMenuByRole(RoleVO roleVO)
  {
    return queryForList("select  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM ms_sec_role_menu AS srm LEFT JOIN ms_sec_menu AS sm ON sm.menu_id = srm.menu_id  WHERE srm.role_id = :roleId AND srm.is_valid = 1 and sm.is_valid = 1 ORDER BY sm.sort asc", roleVO, MenuVO.class);
  }
  
  public List<MenuVO> queryMenuByRole(List<String> roleIdList)
  {
    MapSqlParameterSource patam = new MapSqlParameterSource();
    patam.addValue("roleIdList", roleIdList);
    return queryForList("select  distinct  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM ms_sec_role_menu AS srm ,  ms_sec_menu AS sm  where  sm.menu_id = srm.menu_id  and  srm.role_id in ( :roleIdList) AND srm.is_valid = 1 and sm.is_valid = 1  ORDER BY sm.sort  asc ", patam, MenuVO.class);
  }
  
  public List<MenuVO> queryAllMenu()
  {
    return queryForList("select  sm.menu_id,sm.parent_menu_id,sm.menu_name,sm.menu_desc,sm.created_by,sm.created_time,sm.updated_by,sm.updated_time,sm.is_valid,sm.sort,sm.icon FROM  ms_sec_menu AS sm where is_valid = 1 order by  sort  asc ", null, MenuVO.class);
  }
  
  public int deleteRoleMenu(RoleMenuVO roleMenuVO)
  {
    return save("update ms_sec_role_menu set is_valid = 0 where role_id = :roleId and menu_id = :menuId", roleMenuVO);
  }
  
  public int updateRoleMenu(RoleMenuVO roleMenuVO)
  {
    return save("update ms_sec_role_menu set is_valid = 1 where role_id = :roleId and menu_id = :menuId", roleMenuVO);
  }
  
  public int insertRoleMenu(RoleMenuVO roleMenuVO)
  {
    return save("insert into ms_sec_role_menu(role_id,menu_id,created_by,created_time,updated_by,updated_time,is_valid,sys_code) values(:roleId,:menuId,:createdBy,:createdTime,:updatedBy,:updatedTime,:isValid,:sysCode)", roleMenuVO);
  }
  
  public int getRoleMenuCount(RoleMenuVO roleMenuVO)
  {
    return queryForInt("select count(1) from ms_sec_role_menu where role_id = :roleId and menu_id = :menuId", roleMenuVO);
  }
}
